package com.example.demo.controller;

import ch.qos.logback.core.util.StringUtil;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/user")
@ResponseBody
public class RegisterController {
    @PostMapping("/login")
    public Boolean login(String userName, String password, HttpSession session){
        if (!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)){
            return false;
        }
        if (!"admin".equals(userName) || !"admin".equals(password)){
            return false;
        }
        session.setAttribute("userName",userName);
        return true;

    }

    @GetMapping("getLoginUser")
    public String getUsername(HttpSession session){
        String userName = (String)session.getAttribute("userName");
        return userName;
    }
}
